package codeRandomThoughts.Test213打家劫舍II;

public class Solution {
    public int rob(int[] nums) {
        if (nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        int result1 = robByRange(nums, 0, nums.length - 1);
        int result2 = robByRange(nums, 1, nums.length);
        return Math.max(result1, result2);
    }

    /**
     * 左闭右开区间
     *
     * @param nums
     * @param start
     * @param end
     * @return
     */
    public int robByRange(int[] nums, int start, int end) {
        if (start == end) return nums[start];
        int len = end - start;
        int[] dp = new int[len];
        dp[0] = nums[start];

        for (int i = start + 1; i < end; i++) {
            int tem = 0;
            if (i - start >= 2) {
                tem = dp[i - start - 2];
            }
            dp[i - start] = Math.max(tem + nums[i], dp[i - start - 1]);
        }

        return dp[len - 1];
    }
}
